package com.sky.controller.admin;

import com.sky.annotation.AutoFill;
import com.sky.dto.DishPageQueryDTO;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.mapper.SetMealDishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Api(tags="套餐相关接口")
@Slf4j
public class SetMealController {
    @Autowired
    private SetMealService setMealService;
    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增套餐")
    //此时的category_id表示人气 套餐/商务套餐这种分类id
    @CacheEvict(cacheNames = "setmealCache",key="#setmealDTO.categoryId")
    public Result addMeal(@RequestBody SetmealDTO setmealDTO){
        //添加setMeal表
        setMealService.addMeal(setmealDTO);
        return Result.success();
     }

    @GetMapping("/page")
    @ApiOperation("套餐分页查询")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("套餐分页查询:{}",setmealPageQueryDTO);
        PageResult pageResult=setMealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);

    }

    /**
     * 批量删除套餐
     */
    @DeleteMapping
    @ApiOperation("批量删除套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries=true)
    public Result deleteBatch(@RequestParam List<Long> ids){
        setMealService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 改变套餐起售/停售状态
     */
    @PostMapping("/status/{status}")
    @ApiOperation("改变套餐状态")
    //如果要单独查categpryId需要去数据库中查,这样就与存到redis初衷相悖
    @CacheEvict(cacheNames = "setmealCache",allEntries=true)
    public Result changeStatus(@PathVariable Integer status,Long id){
        setMealService.changeStatus(status,id);
        return Result.success();
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealVO> findById(@PathVariable Long id){
        SetmealVO setmealVO=setMealService.findById(id);
        return Result.success(setmealVO);
    }

    /**
     * 根据id修改套餐
     */
    @PutMapping
    @ApiOperation("根据id修改套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries=true)
    public Result update(@RequestBody SetmealDTO setmealDTO){
        setMealService.updateSetMeal(setmealDTO);
        return Result.success();
    }
}
